package cn.iocoder.yudao.module.hs.dal.mysql.tray;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.hs.controller.admin.tray.vo.TrayPageReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.tray.TrayDO;
import cn.iocoder.yudao.module.hs.enums.warehouse.WarehouseStatusEnum;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;

/**
 * 托盘信息 Mapper
 *
 * @author 航盛
 */
@Mapper
public interface TrayMapper extends BaseMapperX<TrayDO> {

    default PageResult<TrayDO> selectPage(TrayPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TrayDO>()
                .likeIfPresent(TrayDO::getCode, reqVO.getCode())
                .likeIfPresent(TrayDO::getRfidCode, reqVO.getRfidCode())
                .eqIfPresent(TrayDO::getWarehouseId, reqVO.getWarehouseId())
                .eqIfPresent(TrayDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(TrayDO::getWarehousingTime, reqVO.getWarehousingTime())
                .betweenIfPresent(TrayDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(TrayDO::getId));
    }

    default void updateTrayEntryByCode(String code, String rfidCode, Long warehouseId, Long deptId) {
        LambdaUpdateWrapper<TrayDO> updateWrapper = new LambdaUpdateWrapper<TrayDO>()
                .set(TrayDO::getStatus, WarehouseStatusEnum.ENTRY.getStatus())
                .set(TrayDO::getWarehousingTime, LocalDateTime.now())
                .set(TrayDO::getWarehouseId, warehouseId)
                .set(TrayDO::getDeptId, deptId)
                .set(StrUtil.isNotEmpty(rfidCode), TrayDO::getRfidCode, rfidCode)
                .eq(TrayDO::getCode, code);
        update(updateWrapper);
    }
}